import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:sprintf/sprintf.dart';
import 'package:suxuanapp/utils/md5.dart';
/**
 * 扩展雷
 */

/// 字符串扩展方法
extension StringExtension on String{
  /// 是否是电话号码
  bool get isMobileNumber {
    if(this?.isNotEmpty != true) return false;
    return RegExp(r'^((13[0-9])|(14[5,7,9])|(15[^4])|(18[0-9])|(17[0,1,3,5,6,7,8])|(19)[0-9])\d{8}$').hasMatch(this);
  }
  /// 是否是电话号码
  String   format(List args) {
    return sprintf(this,args);
  }
  /// 是否是电话号码
  String   replaceNull(  [  String args=""]) {
    // 确认变量值不为空。
    if(args==null){
      args="";
    }
    if(this==null){
      return args;
    }
    return this;
  }
  /// 转换unicode 编码
  String   toMd5(     ) {
    return   generateMd5(this);
  }

}

/// 转换unicode 编码
String   toUnicode(    String args) {
  var bytes = utf8.encode(args);
  var urlBase = base64Encode(bytes);
  String newStr = utf8.decode(base64Decode(urlBase));
  return newStr  ;
}




/**
 * 减少嵌套
 */
extension WidgetExt on Widget {
  Container intoContainer({
    //复制Container构造函数的所有参数（除了child字段）
    Key key,
    AlignmentGeometry alignment,
    EdgeInsetsGeometry padding,
    Color color,
    Decoration decoration,
    Decoration foregroundDecoration,
    double width,
    double height,
    BoxConstraints constraints,
    EdgeInsetsGeometry margin,
    Matrix4 transform,
  }) {
    //调用Container的构造函数，并将当前widget对象作为child参数
    return Container(
      key: key,
      alignment: alignment,
      padding: padding,
      color: color,
      decoration: decoration,
      foregroundDecoration: foregroundDecoration,
      width: width,
      height: height,
      constraints: constraints,
      margin: margin,
      transform: transform,
      child: this,
    );
  }
}